//
//  MCBaseNetWork.m
//  MCOCModule
//
//  Created by mingci on 2021/10/19.
//

#import "MCBaseNetWork.h"

#import <AFNetworking/AFNetworking.h>


typedef void(^successResult)(id responseObject);

typedef void(^failureResult)(NSError *error);



@interface MCBaseNetWork ()


@property (nonatomic, strong) AFHTTPSessionManager *httpSessionManager;

@property (nonatomic, strong) NSOperationQueue *queue;

@end


@implementation MCBaseNetWork


- (instancetype)mc_singleTon{
    
    static MCBaseNetWork *_baseNetWork = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _baseNetWork = [[MCBaseNetWork alloc] init];
    });
    
    return _baseNetWork;
}




- (instancetype)init{
    if (self = [super init]) {
        _httpSessionManager = [AFHTTPSessionManager manager];
        
    }
    return self;
}


/// get 请求头
/// @param url 网络路径
/// @param parameters 参数
/// @param headers 请求头
- (void)mc_getUrlPath:(NSString *)url withParameters:(NSDictionary *)parameters withHeaders:(NSDictionary *)headers withSuccessResult:( nullable void(^)(id responseObject))successResult withFailure:(nullable void(^)(NSError *error))failure{
    
    [_httpSessionManager GET:url parameters:parameters headers:headers progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        successResult(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
    
}


/// post 请求
/// @param url 网络路径
/// @param paramsters 参数
/// @param header 请求头
- (void)mc_postWithUrl:(NSString *)url withParamsters:(NSDictionary *)paramsters withHeaders:(NSDictionary *)header withSuccessResult:(nullable void(^)(id responseObject))successResult withFailure:(nullable void(^)(NSError *error))failure{
    [_httpSessionManager POST:url parameters:paramsters headers:header progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        successResult(successResult);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}


- (void)mc_mutiUploadWithUrl:(NSString *)url withParameters:(NSDictionary *)paramters withHeaders:(NSDictionary *)headers withImageArr:(NSArray <UIImage *>*)imageArr withSuccessResult:(nullable void(^)(id responseObject))successResult withFailure:(nullable void(^)(NSError *error))failure{
    
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    self.queue = queue;
    
    queue.maxConcurrentOperationCount = 4;
    
    for (int i = 0; i < imageArr.count; i++) {
        
        @autoreleasepool {
            
            __weak typeof(self) weakself = self;
            
            NSBlockOperation *uploadOperation = [NSBlockOperation blockOperationWithBlock:^{
                [weakself mc_uploadWithUrl:url withParameters:paramters withHeaders:headers withImage:imageArr[i] withSuccessResult:successResult withFailure:failure];
            }];
            
            [queue addOperation:uploadOperation];
            
        }
        
    }

    
    
    
    
}

/// 上传接口
/// @param url 网路路径
/// @param parameters 参数
/// @param header 请求头
- (void)mc_uploadWithUrl:(NSString *)url withParameters:(NSDictionary *)parameters withHeaders:(NSDictionary *)header withImage:(UIImage *)image withSuccessResult:(nullable void(^)(id responseObject))successResult withFailure:(nullable void(^)(NSError *error))failure{
    
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    
    [_httpSessionManager POST:url parameters:parameters headers:header constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
    
        NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
        
        NSString *imageName = [NSString stringWithFormat:@"image%f",[NSDate date].timeIntervalSince1970];
        
        [formData appendPartWithFileData:imageData name:@"file" fileName:imageName mimeType:@"image/jpeg"];
        
        } progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            dispatch_semaphore_signal(semaphore);
            successResult(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            dispatch_semaphore_signal(semaphore);
            failure(error);
    }];
    
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    
}


//- (void)mc_deleteWithUrl:(NSString *)url withParameters:(id)parameters withHeaders:(NSDictionary<NSString *, NSString *> *)headers {
//
//}




@end
